package com.pentajock.docdb.dialogs;

import com.pentajock.docdb.Main;
import com.pentajock.docdb.ClientWindow;
import com.pentajock.docdb.images.DBImages;
import com.pentajock.docdb.lang.Lang;
import com.pentajock.docdb.utils.Utils;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SearchDialog extends JDialog {
	static private final String CANCEL_DIALOG_INPUT_KEY = "CANCEL_DIALOG";
	
	static public final int NO_MODE_SELECTED = 0;
	static public final int MATCH_WORDS = 1;
	static public final int MATCH_PHRASE = 2;
	static public boolean SEARCH_FILE_SERVERS;
	
	private JTextField searchField;
	private JRadioButton matchWordsButton;
	private JRadioButton matchPhraseButton;
	private JButton okButton;
	private JButton cancelButton;
	private JCheckBox searchServersCheckBox;
	
	private ClientWindow window;
	private boolean dialogCanceled;
	
	public SearchDialog(ClientWindow window) {
		super(window, Lang.DIALOG.getText("SEARCH_DOCUMENTS", "Search Documents"), true);
		Utils.setIconImage(this, DBImages.SEARCH_BUTTON.getImage());
		this.window = window;
		dialogCanceled = false;
		
		initializeObjects();
		layoutFrame();
		addListeners();
		
		getRootPane().setDefaultButton(okButton);
		setResizable(false);
		pack();
		setLocationRelativeTo(window);
	}
	
	private void initializeObjects() {
		SEARCH_FILE_SERVERS = Main.userIni.getBooleanProperty("Preferences", "search_file_servers");
		
		searchField = new JTextField(20);
		okButton = new JButton(Lang.BUTTON.getText("OK", "Ok"));
		okButton.setDefaultCapable(true);
		okButton.setMnemonic(Lang.BUTTON.getMnemonic("OK", KeyEvent.VK_O));
		cancelButton = new JButton(Lang.BUTTON.getText("CANCEL", "Cancel"));
		cancelButton.setMnemonic(Lang.BUTTON.getMnemonic("CANCEL", KeyEvent.VK_C));
		matchWordsButton = new JRadioButton(Lang.DIALOG.getText("MATCH_WORDS", "Match individual words"), true);
		matchWordsButton.setMnemonic(Lang.DIALOG.getMnemonic("MATCH_WORDS", KeyEvent.VK_W));
		matchWordsButton.setToolTipText(Lang.TOOL_TIP.get("MATCH_WORDS", "Search for each word"));
		matchPhraseButton = new JRadioButton(Lang.DIALOG.getText("MATCH_PHRASE", "Match entire phrase"), false);
		matchPhraseButton.setMnemonic(Lang.DIALOG.getMnemonic("MATCH_PHRASE", KeyEvent.VK_P));
		matchPhraseButton.setToolTipText(Lang.TOOL_TIP.get("MATCH_PHRASE", "Search for entire phrase"));
		searchServersCheckBox = new JCheckBox(Lang.DIALOG.getText("SEARCH_SERVERS", "Search file servers"), SEARCH_FILE_SERVERS);
		searchServersCheckBox.setMnemonic(Lang.DIALOG.getMnemonic("SEARCH_SERVERS", KeyEvent.VK_S));
		searchServersCheckBox.setToolTipText(Lang.TOOL_TIP.get("SEARCH_SERVERS", "Search additional servers"));
		
		ButtonGroup group = new ButtonGroup();
		group.add(matchWordsButton);
		group.add(matchPhraseButton);
	}
	
	private void layoutFrame() {
		SpringLayout layout = new SpringLayout();
		JPanel searchPanel = new JPanel(layout);
		JLabel searchLabel = new JLabel(Lang.BUTTON.getText("SEARCH", "Search") + ":");
		searchLabel.setDisplayedMnemonic(Lang.BUTTON.getDisplayedMnemonic("SEARCH", 'S'));
		searchLabel.setLabelFor(searchField);
		
		searchPanel.add(searchLabel);
		searchPanel.add(searchField);
		searchPanel.add(matchWordsButton);
		searchPanel.add(matchPhraseButton);
		searchPanel.add(searchServersCheckBox);
		
		layout.putConstraint(SpringLayout.NORTH, searchLabel, 10, SpringLayout.NORTH, searchPanel);
		layout.putConstraint(SpringLayout.WEST, searchLabel, 10, SpringLayout.WEST, searchPanel);
		layout.putConstraint(SpringLayout.NORTH, searchField, 10, SpringLayout.NORTH, searchPanel);
		layout.putConstraint(SpringLayout.WEST, searchField, 5, SpringLayout.EAST, searchLabel);
		layout.putConstraint(SpringLayout.NORTH, matchWordsButton, 10, SpringLayout.SOUTH, searchLabel);
		layout.putConstraint(SpringLayout.WEST, matchWordsButton, 10, SpringLayout.WEST, searchPanel);
		layout.putConstraint(SpringLayout.NORTH, matchPhraseButton, 0, SpringLayout.SOUTH, matchWordsButton);
		layout.putConstraint(SpringLayout.WEST, matchPhraseButton, 10, SpringLayout.WEST, searchPanel);
		
		layout.putConstraint(SpringLayout.NORTH, searchServersCheckBox, 0, SpringLayout.SOUTH, matchPhraseButton);
		layout.putConstraint(SpringLayout.WEST, searchServersCheckBox, 10, SpringLayout.WEST, searchPanel);
		
		layout.putConstraint(SpringLayout.SOUTH, searchPanel, 5, SpringLayout.SOUTH, searchServersCheckBox);
		layout.putConstraint(SpringLayout.EAST, searchPanel, 10, SpringLayout.EAST, searchField);
		
		JPanel buttonPanel = new JPanel(new FlowLayout());
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		
		getContentPane().add(searchPanel, BorderLayout.CENTER);
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
	}
	
	private void addListeners() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				cancelDialog();
			}
		});
		
		addComponentListener(new ComponentAdapter() {
			public void componentShown(ComponentEvent e) {
				searchField.requestFocusInWindow();
			}
		});
		
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cancelDialog();
			}
		});
		
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				acceptDialog();
			}
		});
		
		InputMap inputMap = new ComponentInputMap((JComponent)getContentPane());
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_DIALOG_INPUT_KEY);
		
		ActionMap actionMap = new ActionMap();
		actionMap.put(CANCEL_DIALOG_INPUT_KEY, new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				cancelDialog();
			}
		});
		
		((JComponent)getContentPane()).setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
		((JComponent)getContentPane()).setActionMap(actionMap);
	}
	
	private void acceptDialog() {
		SEARCH_FILE_SERVERS = searchServersCheckBox.isSelected();
		Main.userIni.setBooleanProperty("Preferences", "search_file_servers", SEARCH_FILE_SERVERS);
		dialogCanceled = false;
		setVisible(false);
	}
	
	private void cancelDialog() {
		dialogCanceled = true;
		setVisible(false);
	}
	
	public boolean isCanceled() {
		return dialogCanceled;
	}
	
	public int getMatchMode() {
		if(matchWordsButton.isSelected()) {
			return MATCH_WORDS;
		}
		if(matchPhraseButton.isSelected()) {
			return MATCH_PHRASE;
		}
		return NO_MODE_SELECTED;
	}
	
	public String getSearchPhrase() {
		if(dialogCanceled) {
			return null;
		}
		return searchField.getText();
	}
	
	public void setVisible(boolean b) {
		if(b) {
			searchField.setText("");
			matchWordsButton.setSelected(true);
			setLocationRelativeTo(window);
		}
		super.setVisible(b);
	}
}
